#!/usr/bin/env node

const http = require('http'),
  log = console.log,
  print = require('util').debuglog('dev'),
  cheerio = require('cheerio'),
  baseURL = 'http://ke.segmentfault.com',
  url = 'http://ke.segmentfault.com/free';


http.get(url,(res) => {
  let result = '';

  res.on('data', (data) => {
    result += data.toString('utf8');
    console.log(result);
  });
  res.on('end',() => {
    print(result);//!!!!
    //console.log('-------------');

    let $ = cheerio.load(result);
    $('body').find('.card-title>a').each(function(){
      print($(this).html());
      //console.log('-------------');
      let cName = $(this).text(),
        cURL = baseURL + $(this).attr('href');

      if(cName === '')
        return;

      console.log('课程名称：this.text',cName);
      console.log('课程网址：cURL.trim()',cURL.trim());
      console.log('');
    });
  });
});


